package com.ztesoft.book.core.quartz.schedulejoblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztesoft.book.common.utils.query.PageBO;
import com.ztesoft.book.common.utils.query.Query;
import com.ztesoft.book.core.quartz.schedulejob.util.ScheduleUtils;
import com.ztesoft.book.core.quartz.schedulejoblog.dto.ScheduleJobLogDTO;
import com.ztesoft.book.core.quartz.schedulejoblog.mapper.ScheduleJobLogMapper;
import com.ztesoft.book.core.quartz.schedulejoblog.service.ScheduleJobLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Description 定时调度日志业务类
 * @Author YangFan
 * @Date 2021-04-15 15:34
 */
@Service("scheduleJobLogService")
public class ScheduleJobLogServiceImpl extends ServiceImpl<ScheduleJobLogMapper, ScheduleJobLogDTO> implements ScheduleJobLogService {

    @Resource
    private ScheduleJobLogMapper scheduleJobLogMapper;

    @Override
    public PageBO queryPage(Map<String, Object> params) {
        String jobId = (String) params.get("jobId");

        IPage<ScheduleJobLogDTO> page = this.page(
                new Query<ScheduleJobLogDTO>().getPage(params),
                new QueryWrapper<ScheduleJobLogDTO>().like(StringUtils.isNotBlank(jobId), "job_id", jobId)
        );

        return new PageBO(page);
    }


    @Override
    public void validateAndDeleteJob(String jobId, String cronExpression) {
        // 如果定时任务已不可以执行，则删除任务（这里只删除非循环任务）
        if (!ScheduleUtils.isValidateCanDoExpression(cronExpression)) {
            scheduleJobLogMapper.deleteScheduleJob(jobId);
        }
    }

}
